<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl"/>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#-- 往数据模型中填充一个属性 -->
            <#assign menu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                关键字<input type="text" name="keyword" value="${qo.keyword!}">

                <a role="button" class="btn btn-success searchBtn">
                    <span class="glyphicon glyphicon-search"></span>查询
                </a>
                <a role="button" class="btn btn-success updateBtn">
                    <span class="glyphicon glyphicon-pencil"></span>添加
                </a>
            </form>
            <table class="table table-striped table-hover" >
                <thead>
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                </tr>


                </thead>
                <#list pageInfo.list as entity>
                    <tr>
                       <td>${entity_index + 1}</td>
                       <td>${entity.title}</td>
                       <td><img src="${entity.coverUrl}" width="30px"></td>
                       <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm:ss"))!}</td>
                       <td>${entity.sequence}</td>
                       <td>${entity.catalog.name}</td>
                       <td>${entity.stateName}</td>
                        <td>
                            <a role="button" class="btn btn-xs btn-success updateBtn" data-json='${entity.json!}'>
                                <span class="glyphicon glyphicon-pencil"></span>修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增攻略文章</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id" />
                    <input type="hidden" name="coverUrl">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">

                            <input type="text" class="form-control" name="title"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="img" width="100%">
                            <input type="file" class="form-control" name="file"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select name="catalog.strategy.id" class="form-control" id="s1">
                                <#list strategys as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select name="catalog.id" class="form-control" id="s2">
                               <#-- <#list strategys as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>-->
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sequence"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <textarea id="editor1" name="content.content"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary strategyDetailSubmitBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        var ck = CKEDITOR.replace('editor1');
        //给查询按钮绑定点击事件
       $(".searchBtn").click(function () {
           //提交表单
           $("#searchForm").submit();
       });
       //修改按钮的点击事件
       $(".updateBtn").click(function () {
           $("#editForm input").val("");
           $("#editForm select").val("");
           $("#img").attr("src","");
           var json = $(this).data("json");
           ck.setData("");
           if (json) {
               $("#editForm input[name='title']").val(json.title);
               $("#editForm input[name='id']").val(json.id);
               $("#editForm input[name='sequence']").val(json.sequence);
               $("#editForm input[name='coverUrl']").val(json.coverUrl);
               $("#editForm select[name='catalog.strategy.id']").val(json.StrategyId);
               $("#editForm select[name='state']").val(json.state);
               //二级联动
               //$("#editForm select[name='state']").val(json.state"");
               $("#img").attr("src",json.coverUrl);
               $.get("/strategyCatalog.do",{id:json.StrategyId},function (data) {
                   //对请求返回的JSON城市信息进行迭代
                   $.each(data,function (index,value) {//index  每一个城市信息的索引
                       //value  每一个封装城市信息的对象
                       //将城市信息中的 id 作为下拉条option的value  name 作为显示内容 拼接到select元素后面
                       c.append("<option value = '"+value.id+"'>"
                               +value.name+"</option>");
                       $("#editForm select[name='catalog.id']").val(json.catalogId);
                   })
                   $.get('/strategyDetail.do',{id:json.id},function (data) {
                       ck.setData(data.content);
                   });
               },"json");

           }
           $("#inputModal").modal("show");

       });
        $("#editForm").ajaxForm(function (data) {
            if (data.success) {
                reload();
            }
        });
        //模态框按钮的点击事件
        $(".strategyDetailSubmitBtn").click(function () {
            var content = ck.getData();
            $("textarea[name='content.content']").val(content);
            $("#editForm").submit();
        });

        var p = $("#s1");
        var c = $("#s2")
        p.change(function () {
            //每次省下拉条变动 清空重置城市下拉条,防止叠加
            c.html("<option value=\"-1\">请选择</option>");
            //获取当前p元素的value
            var val = this.value;
            //大于0表示 选择了省份,此时需要将省份对应城市查询出来并显示
            if(val > 0){
                //发送ajax请求,携带城市的value,返回城市的信息数据
                $.get("/strategyCatalog.do",{id:val},function (data) {
                    //对请求返回的JSON城市信息进行迭代
                    $.each(data,function (index,value) {//index  每一个城市信息的索引
                        //value  每一个封装城市信息的对象
                        //将城市信息中的 id 作为下拉条option的value  name 作为显示内容 拼接到select元素后面
                        c.append("<option value = '"+value.id+"'>"
                                +value.name+"</option>");
                    })
                },"json");
            }
        })

    });
</script>
<style>
    #contentModal img{
        width: 100%;
    }
</style>
</body>
</html>